package com.java.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.dao.MediaMapper;
import com.java.model.Admin;
import com.java.model.Media;
import com.java.model.Result;
import com.java.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MediaServiceImpl implements MediaService {

    @Autowired
    private MediaMapper mediaMapper;
    int yanzheng = 0;
    @Override
    public PageInfo<Media> enterList(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());
        List<Media> list = mediaMapper.enterList(media);
        return new PageInfo<>(list);
    }

    @Override
    public Result yes(Media media) {
        mediaMapper.yes(media);
        return Result.success();
    }

    @Override
    public List<Media> register() {
        List<Media> register = mediaMapper.register();
        return register;
    }

    @Override
    public Result register2(Media media) {
        Media media1 = mediaMapper.checkPhone(media);
        if(media1 == null || media1.equals("")){
            mediaMapper.register2(media);
            return Result.success();
        }
        return Result.error("该手机号已注册");
    }

    @Override
    public PageInfo<Media> list2(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());
        List<Media> list =  mediaMapper.list2(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return new PageInfo<>(list);
    }

    //首页种草媒体显示
    @Override
    public List<Media> list(Media media) {
        List<Media> list =  mediaMapper.list(media);
        return list;
    }
    //首页短视频媒体显示
    @Override
    public List<Media> listVideo(Media media) {
        List<Media> list =  mediaMapper.listVideo(media);
        return list;
    }

    //媒体显示
    @Override
    public List<Media> listTiktok(Media media) {
        List<Media> list =  mediaMapper.listTiktok(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return list;
    }
    //媒体显示
    @Override
    public List<Media> listTiktok2(Media media) {
        List<Media> list =  mediaMapper.list3(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return list;
    }
    @Override
    public Media particulars(Media media) {
        return mediaMapper.particulars(media);
    }

    //粉丝数量排行
    @Override
    public List<Media> listFans(Media media) {
        List<Media> list =  mediaMapper.listFans(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return list;
    }

    //报价排行
    @Override
    public List<Media> listQuote(Media media) {
        List<Media> list =  mediaMapper.listQuote(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return list;
    }

    //阅读排行
    @Override
    public List<Media> listReading(Media media) {
        List<Media> list =  mediaMapper.listReading(media);
        for (Media me :list){
            Long fansAmount = me.getFansAmount();
            if (fansAmount >= 10000){
                String amount = (fansAmount / 10000) + "w";
                me.setAmount(amount);
            }else if (fansAmount >=  100000000){
                String amount = (fansAmount / 10000) + "亿";
                me.setAmount(amount);
            }else {
                String amount = "";
                amount = amount+fansAmount;
                me.setAmount(amount);
            }

            Long accountPv = me.getAccountPv();
            if (accountPv >= 10000){
                String account = (accountPv / 10000) + "w";
                me.setAccount(account);
            }else if (accountPv >= 100000000){
                String account = (accountPv / 10000) + "亿";
                me.setAccount(account);
            }else {
                String account = "";
                account = account+accountPv;
                me.setAmount(account);
            }
        }
        return list;
    }

    //后台微博
    @Override
    public PageInfo<Media>  listWeibo(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());
        List<Media> list =  mediaMapper.listWeibo(media);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Media> listRedBook(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());
        List<Media> list =  mediaMapper.listRedBook(media);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Media> listDouYin(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());
        List<Media> list =  mediaMapper.listDouYin(media);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Media> listBiliBili(Media media) {
        List<Media> list =  mediaMapper.listBiliBili(media);
        return new PageInfo<>(list);
    }

    @Transactional
    @Override
    public Result modify(Media media) {
        mediaMapper.modify(media);
        return Result.success();
    }

    @Override
    public Media get(int id) {
        Media media = mediaMapper.get(id);
        return media;
    }

    @Transactional
    @Override
    public Result modify2(Media media) {
        mediaMapper.modify2(media);
        return Result.success();
    }

    @Override
    public Media get2(int id) {
        Media media = mediaMapper.get2(id);
        return media;
    }


    @Override
    public List<Media> mediaCount() {
        return mediaMapper.mediaCount();
    }

    @Override
    public List<Media> mediaCountTow() {
        return mediaMapper.mediaCountTow();
    }

    @Override
    public Result list3(Media media) {
        PageHelper.startPage(media.getPageNum(), media.getPageSize());

        List<Media> list = mediaMapper.list3(media);
        PageInfo<Media> pageInfo = new PageInfo(list);

        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public Result state(Media media) {
        if (media.getVerification().equals(yanzheng+"")) {
            Media state = mediaMapper.state(media);
            if (state != null && !state.equals("")){
                return Result.error("管理员正在审核中");
            }else{
                return Result.success();
            }
        }
        return Result.error("验证码输入有误");
    }

}
